package com.daiziru.wuyanmall.controller;

import com.daiziru.wuyanmall.entity.ProductComments;
import com.daiziru.wuyanmall.service.ProductCommentService;
import com.daiziru.wuyanmall.service.ProductService;
import com.daiziru.wuyanmall.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/product")
@Api(value = "提供商品相关信息的接口", tags = "商品管理")

public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private ProductCommentService productCommentService;

    @ApiOperation("商品基本信息查询接口")
    @GetMapping("/detail-info/{productId}")
    public ResultVO getProductBasicInfo(@PathVariable("productId") String pid) {
        return productService.getProductBasicInfo(pid);
    }

    @ApiOperation("商品参数信息查询接口")
    @GetMapping("/detail-params/{productId}")
    public ResultVO getProductParamsById(@PathVariable("productId") String pid) {
        return productService.getProductParamsByPid(pid);
    }

    @ApiOperation("商品评论信息查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int",name = "pageNum", value = "当前页码",required = true),
            @ApiImplicitParam(dataType = "int",name = "limit", value = "每页显示条数",required = true)
    })
    @GetMapping("/detail-comments/{productId}")
    public ResultVO getProductCommentsById(@PathVariable("productId") String pid, int pageNum, int limit) {
        return productCommentService.ListCommentsByProductId(pid,pageNum, limit);
    }
    @ApiOperation("商品评论数查询接口")
    @GetMapping("/detail-commentsCount/{productId}")
    public ResultVO getProductCommentsById(@PathVariable("productId") String pid) {
        return productCommentService.listCommentsCountByProductId(pid);
    }
    @ApiOperation("商品评论接口")
    @PostMapping("/AddComment")
    public ResultVO addShoppingCart(@RequestBody ProductComments comment, @RequestHeader("token") String token){
        ResultVO resultVO = productCommentService.addProductComment(comment);
        return resultVO;
    }

    @ApiOperation("根据类别查询商品接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int",name = "pageNum", value = "当前页码",required = true),
            @ApiImplicitParam(dataType = "int",name = "limit", value = "每页显示条数",required = true)
    })
    @GetMapping("/listbycid/{cid}")
    public ResultVO getProductByCategoryId(@PathVariable("cid") int cid, int pageNum, int limit) {
        return productService.getProductsByCategoryId(cid, pageNum, limit);
    }


    @ApiOperation("根据类别id查询商品品牌接口")
    @GetMapping("/listbrands/{cid}")
    public ResultVO getBrandsByCategoryId(@PathVariable("cid") int cid) {
        ResultVO resultVO = productService.listBrands(cid);
        return resultVO;
    }
    @ApiOperation("根据关键字查询商品品牌接口")
    @GetMapping("/listBrands-keyword")
    public ResultVO getBrandsByKeyword(String keyword) {
        ResultVO resultVO = productService.listBrands(keyword);
        return resultVO;
    }

    @ApiOperation("根据品牌和类别id查询商品接口")
    @GetMapping("/listBybrand/{cid}")
    public ResultVO getBrandsByCategoryId(@PathVariable("cid") int cid, String brand) {
        ResultVO resultVO = productService.getProductByBrand(cid, brand);
        return resultVO;
    }

    @ApiOperation("根据品牌和关键字查询商品接口")
    @GetMapping("/listByBrand-Keyword")
    public ResultVO getBrandsByCategoryId(String keyword, String brand) {
        ResultVO resultVO = productService.getProductByBrand(keyword, brand);
        return resultVO;
    }

    //模糊查询---根据关键字搜索商品
    @ApiOperation("根据关键字查询商品接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String",name = "keyword", value = "搜索关键字",required = true),
            @ApiImplicitParam(dataType = "int",name = "pageNum", value = "当前页码",required = true),
            @ApiImplicitParam(dataType = "int",name = "limit", value = "每页显示条数",required = true)
    })
    @GetMapping("/listProductsByKeyword")
    public ResultVO searchProduct(String keyword, int pageNum, int limit) {
        return productService.selectProduct(keyword, pageNum, limit);
    }

}
